﻿using System;

namespace ArrayByValByRef
{
    class Program
    {   
        /// <summary>
        /// adfadsfasdf
        /// </summary>
        /// <param name="intArray"></param>
        /// <param name="maxIndex"></param>
        /// <returns></returns>
        static int MaxValue(int[] intArray, out int maxIndex)
        {
            int maxVal = intArray[0];
            maxIndex = 0;
            for (int i = 1; i < intArray.Length; i++)
            {
                if (intArray[i] > maxVal)
                {
                    maxVal = intArray[i];
                    maxIndex = i;
                }
            }
            return maxVal;
        }

        static void ChangeByVal(int[] pArray)
        {
            pArray[0] = 888;  // 修改形参pArray[0]的值
            pArray = new int[5] { -3, -1, -2, -3, -4 };   // 重新分配形参pArray的内存空间，并赋值
            System.Console.WriteLine("函数内形参pArray[0]的值: {0}", pArray[0]);
        }
        static void ChangeByRef(ref int[] pArray)
        {
            pArray[0] = 888;// 修改形参pArray[0]的值
            pArray = new int[5] { -3, -1, -2, -3, -4 };// 重新分配形参pArray的内存空间，并赋值
            System.Console.WriteLine("函数内形参pArray[0]的值: {0}", pArray[0]);
        }
        static void Main()
        {
            //定义数组arr，并按值传递给函数ChangeByVal
            int[] arr = { 1, 4, 5 };
            System.Console.WriteLine("Main函数内，按值传递前实参arr[0]的值: {0}", arr[0]);
            ChangeByVal(arr);
            System.Console.WriteLine("Main函数内，按值传递调用函数后实参arr[0]的值: {0}", arr[0]);

            System.Console.WriteLine();
            System.Console.WriteLine("_________________________________________________");
            System.Console.WriteLine();
           
            //定义数组arr1，并按引用传递给函数ChangeByRef
           int[] arr1 = { 1, 4, 5 };
            System.Console.WriteLine("Main函数内，按应用传递前实参arr1[0]的值: {0}", arr1[0]);

            ChangeByRef(ref arr1);
            System.Console.WriteLine("Main函数内，按值传递调用函数后实参arr1[0]的值: {0}", arr1[0]);

        }
    }
}
